﻿
// Order ViewModel
function Order(model) {

    RajonViewModel.prototype.constructor.apply(this);
    this.model = model;

    this.TotalPrice = function () {
        var total = 0;
        for (var i = 0, li = this.model.Items.length; i < li; i++)
            total = total + this.model.Items[i].Quantity * this.model.Items[i].ItemPrice;

        return total;
    }

    this.EditUrl = function () {
        if (this.model == null)
            return "#";
        return "SimpleCRUD.htm?OrderNumber=" + this.model.OrderNumber;
    }

    this.AlternateItemStyle = function () {
        var list = Rajon.getParentViewModel(this.getView());
        var idx = this.model._modelIndex + (list.pageSize * (list.getPageNumber()-1));
        if (idx % 2 != 0)
            return "alternateItem";
        else
            return "normalItem";
    }

    this.onCommitClientName = function (value) {
        if (value == 'BAD GUY')
            throw Rajon.Resources.Not_valid_client;
    }

    this.onCommitDeliveryDate = function (value) {
        var now = new Date();
        if (value < now)
            throw Rajon.Resources.Not_valid_date;
    }
    
}

Order.prototype = new RajonViewModel();
